﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client;
using Yugi_Gui.Util_Control;
using Yugi.Client.ViewModel;
namespace Yugi_Gui.COntrol
{
    /// <summary>
    /// Interaction logic for ChatControl.xaml
    /// </summary>
    public partial class ChatControl : UserControl, IInvoke
    {
        Canvas can;
        Point last = new Point(100, 100);
        ChatModel model;
        MainServerHandler main;
        public ChatControl(MainServerHandler m,bool isChannel)
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ChatControl_Loaded);
            this.Unloaded += new RoutedEventHandler(ChatControl_Unloaded);
            this.MouseMove += new MouseEventHandler(ChatControl_MouseMove);

            main = m;
            if (isChannel == false)
            {
                model = main.ChatManager_Model.ListChat[main.ChatManager_Model.ListChat.Count - 1];
                InitModel(model);
                model.type = ChatType.OneOne;
            }
           
        }

        public void InitModel(ChatModel model1)
        {
            model = model1;
            model.Presenter = this;
            model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(model_TextChanged);
            this.txtTo.Text = model.to;
            this.txtContent.Text = model.Content;
           // model.type = ChatType.World;
        }
        void ChatControl_Unloaded(object sender, RoutedEventArgs e)
        {
            main.ChatManager_Model.Remove(model.to);
        }

        void model_TextChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.txtContent.Text = model.Content;
        }

        void ChatControl_Loaded(object sender, RoutedEventArgs e)
        {

        }


        void ChatControl_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(this.Parent as UIElement);

            if (e.LeftButton == MouseButtonState.Pressed)
            {

                double left = Canvas.GetLeft(this);
                double top = Canvas.GetTop(this);
                Canvas.SetLeft(this, left + p.X - last.X);
                Canvas.SetTop(this, top + p.Y - last.Y);

            }
            last = p;
        }

        public void Add(Canvas cv)
        {
            Canvas.SetTop(this, 100);
            Canvas.SetLeft(this, 100);
            cv.Children.Add(this);
            can = cv;
        }


        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            if (model.type == ChatType.OneOne)
            {
                model.AddText(model.from, this.txtText.Text);
                model.main.SendMessageToServer(Common.MSG_GSM.L_Chat_OK, model.from, model.to, txtText.Text);
            }
            else
            {
              //  model.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(model_TextChanged);
                model.SendTextChannel(this.txtText.Text);
              //  model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(model_TextChanged);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            can.Children.Remove(this);
        }

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch (msg)
            {
                case ClientMSG.Chat_OK:
                    model.AddText((string)obj[0], (string)obj[2]);
                    break;
                case ClientMSG.Chat_Error:
                    model.AddText((string)obj[2]);
                    break;
            }
        }
        public void Refresh()
        {
           
        }
    }
}
